home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks '96 / Internet Chooser / reggie / light / socket.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-21  |  8.9 KB  |  270 lines  |  [TEXT/MMCC]

  1. /* File "socket.h", Light Sockets - Copyright (C) Matt Slot, 1996             */
  2. /* Foundation layer for the Light Sockets networking abstraction routines.    */
  3.  
  4. #ifndef __SOCKET_HEADER__
  5. #define __SOCKET_HEADER__
  6.  
  7. #ifndef __STD_TYPES_HEADER__
  8. #include "stdtypes.h"
  9. #endif /* __STD_TYPES_HEADER__ */
  10.  
  11. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  12. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  13. /* Preprocessor Defines */
  14.  
  15. /* Primarily private defines */
  16. #define qGetSocketProtocol(st)    (st & eSocketProtocolMask)
  17. #define qIsSocketTypeTCPIP(st)    (qGetSocketProtocol(st) == eSocketProtocolTCPIP)
  18. #define qIsSocketTypeATalk(st)    (qGetSocketProtocol(st) == eSocketProtocolATalk)
  19. #define qIsSocketTypeIPX(st)    (qGetSocketProtocol(st) == eSocketProtocolIPX)
  20. #define qIsSocketTypeRPC(st)    (qGetSocketProtocol(st) == eSocketProtocolRPC)
  21. #define qIsSocketTypeFile(st)    (qGetSocketProtocol(st) == eSocketProtocolFile)
  22.         
  23. #define qGetSocketTransfer(st)    (st & eSocketTransferMask)
  24. #define qIsSocketTypeDatagram(st)    \
  25.         (qGetSocketTransfer(st) == eSocketTransferDatagram)
  26. #define qIsSocketTypeStream(st)    \
  27.         (qGetSocketTransfer(st) == eSocketTransferStream)
  28. #define qIsSocketTypeXaction(st)    \
  29.         (qGetSocketTransfer(st) == eSocketTransferXaction)
  30.  
  31. /* Primarily public defines */
  32. #define qIsSocketTypeRawIP(st)        \
  33.         (((st & eSocketTypeMask) == eSocketTypeRawIP) ? true : false)
  34. #define qIsSocketTypeUDP(st)        \
  35.         (((st & eSocketTypeMask) == eSocketTypeUDP) ? true : false)
  36. #define qIsSocketTypeTCP(st)        \
  37.         (((st & eSocketTypeMask) == eSocketTypeTCP) ? true : false)
  38.  
  39. #define qIsSocketTypeDDP(st)        \
  40.         (((st & eSocketTypeMask) == eSocketTypeDDP) ? true : false)
  41. #define qIsSocketTypeADSP(st)        \
  42.         (((st & eSocketTypeMask) == eSocketTypeADSP) ? true : false)
  43.  
  44. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  45.  
  46. #define    kSocketNoErrorStr        "No Error"
  47. #define    kSocketErrUnknownStr    "Unknown Error"    
  48. #define    kSocketErrInternalStr    "Internal Error"    
  49. #define    kSocketErrBadParamStr    "Bad Parameter"    
  50. #define    kSocketErrOutOfMemStr    "Out of Memory"    
  51. #define    kSocketErrNoSupportStr    "Operation Not Supported"    
  52. #define    kSocketErrNoStackStr    "Network Stack Missing"    
  53. #define    kSocketErrNotLoadedStr    "Network Stack Not Loaded"    
  54. #define    kSocketErrCantLoadStr    "Network Stack Couldn't Load"    
  55. #define    kSocketErrBadStackStr    "Network Stack Invalid"    
  56. #define    kSocketErrCantBindStr    "Can't Bind to Address"    
  57. #define    kSocketErrWasBoundStr    "Socket already bound"    
  58. #define    kSocketErrFloodedStr    "Socket Buffers Exhausted"    
  59.  
  60. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  61.  
  62. #undef    AF_INET
  63. #define    AF_INET 2
  64.  
  65. #undef    PF_UDP
  66. #define    PF_UDP  17
  67.  
  68. #undef    PF_TCP
  69. #define    PF_TCP  6
  70.  
  71. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  72.  
  73. //#define    kSocketLoggingSize    4096
  74. #define    kSocketLoggingSize    2048
  75.  
  76. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  77. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  78. /* Enum/Structure/Class Definitions */
  79.  
  80.  
  81. typedef SInt32 SocketResult; 
  82.  
  83. enum {
  84.     eSocketNoError =              0,
  85.     eSocketErrBase =          -7000,
  86.     
  87.     /* Private components */
  88.     eSocketErrUnknown =     eSocketErrBase -  0,    /* Unknown, Unspecified */
  89.     eSocketErrInternal =     eSocketErrBase -  1,    /* Private, Corruption */
  90.     eSocketErrBadParam =     eSocketErrBase -  2,    /* Bad Parameter */
  91.     eSocketErrOutOfMem =     eSocketErrBase -  3,    /* Out of Memory */
  92.     eSocketErrNoSupport =     eSocketErrBase -  4,    /* Op not supported */
  93.     eSocketErrNoStack =     eSocketErrBase -  5,    /* No network stack */
  94.     eSocketErrNotLoaded =     eSocketErrBase -  6,    /* Stack not loaded */
  95.     eSocketErrCantLoad =     eSocketErrBase -  7,    /* Can't load stack */
  96.     eSocketErrBadStack =     eSocketErrBase -  8,    /* Network stack invalid */
  97.     eSocketErrCantBind =     eSocketErrBase -  9,    /* Can't bind to address */
  98.     eSocketErrWasBound =     eSocketErrBase - 10,    /* Socket already bound */
  99.     eSocketErrFlooded =     eSocketErrBase - 11        /* No more buffers */
  100.     };
  101.  
  102. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  103.  
  104. typedef SInt32 SocketType; 
  105.  
  106. enum {
  107.     eSocketUninitialized =         0x00000000,
  108.  
  109.     eSocketProtocolTCPIP =         0x00000001,
  110.     eSocketProtocolATalk =         0x00000002,
  111.     eSocketProtocolIPX =         0x00000003,
  112.     eSocketProtocolRPC =         0x00000004,
  113.     eSocketProtocolFile =         0x00000005,
  114.     
  115.     eSocketTransferDatagram =    0x00000100,
  116.     eSocketTransferStream =        0x00000200,
  117.     eSocketTransferXaction =    0x00000300,
  118.  
  119.     eSocketTypeUDP =            0x00010101,
  120.     eSocketTypeRawIP =            0x00020101,
  121.     eSocketTypeTCP =            0x00010102,
  122.     eSocketTypeDDP =            0x00010201,
  123.     eSocketTypeADSP =            0x00010202,
  124.  
  125.     eSocketProtocolMask =        0x000000FF,
  126.     eSocketTransferMask =        0x0000FF00,
  127.     eSocketIdentifierMask =        0x00FF0000,
  128.     eSocketReservedMask =        0xFF000000,
  129.     eSocketTypeMask =            0x00FFFFFF
  130.     };
  131.  
  132. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  133.  
  134. typedef struct SocketAddress {
  135.     UInt32                    type;
  136.     UInt32                    length;
  137.     union {
  138.         Byte8                bytes[ARBITRARY_BUFFER_SIZE];
  139.         struct {
  140.             UInt16            type;
  141.             UInt16            port;
  142.             UInt32            host;
  143.             Byte8            data[8];
  144.             }                tcpip;
  145.         }                     buffer;
  146.     } SocketAddress, *SocketAddressPtr;
  147.  
  148. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  149.  
  150. typedef SocketResult (* SocketSpinProc)(class Socket *socket, void *refcon);
  151.  
  152. typedef SocketResult (* DatagramWriteProc)(class DatagramSocket *socket, 
  153.         void *refcon, Byte8 *dataPtr, UInt32 dataLen, SocketAddressPtr address);
  154. typedef SocketResult (* DatagramReadProc)(class DatagramSocket *socket, 
  155.         void *refcon, Byte8 *dataPtr, UInt32 dataLen, SocketAddressPtr address);
  156.  
  157. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  158.  
  159. typedef class Socket {
  160.  
  161. public:
  162.     virtual                    ~Socket            (void);
  163.  
  164.  
  165.     /* Basic member functions */
  166.     virtual SocketResult    Create            (SocketType socketType);
  167.     virtual SocketResult    Tickle            (void);
  168.     virtual SocketResult    Dispose            (void);
  169.     
  170.     /* Primary member functions */
  171.             SocketResult     Bind            (SocketAddressPtr reqAddress,
  172.                                                 SocketAddressPtr retAddress);
  173.             SocketResult     Unbind            (void);
  174.     
  175.             SocketResult    GetSpinProc        (SocketSpinProc &proc,
  176.                                                 void * &refcon);
  177.             SocketResult    SetSpinProc        (SocketSpinProc proc,
  178.                                                 void *refcon);
  179.  
  180.     /* Public accessor functions */
  181.             SocketResult    GetInstError    (void);
  182.             void            ResetInstError    (void);
  183.  
  184.     /* Network stack accessor functions */
  185.             SocketType        GetSocketType    (void);
  186.     
  187.             Bool8            IsSocketInited    (void);
  188.             void            SetSocketInited    (Bool8 inited);
  189.             Bool8            IsSocketBound    (SocketAddressPtr boundTo);
  190.             void            SetSocketBound    (Bool8 bound,
  191.                                                 SocketAddress &boundTo);
  192.  
  193. #ifdef _DEBUG
  194.     /* Packet logging functions */
  195.             SocketResult    EnableLogging    (char *filename,
  196.                                                 Bool8 replacing=true);
  197.             SocketResult    DisableLogging    (void);
  198.             Bool8            IsLogging        (void);
  199. #endif
  200.  
  201. protected:
  202.  
  203.     /* Protect constructor - Base class can't be instantiated */
  204.                             Socket            (void);
  205.  
  206.     /* Callback member functions */
  207.             SocketResult     CallSpinProc    (void);
  208.  
  209.  
  210.     /* Protected member data */
  211.             SocketResult    instError;        /* Instance error storage  */
  212.             SocketType        socketType;        /* Internal socket information */
  213.     
  214.             Bool8            inited;            /* Has been successfully inited */
  215.             Bool8            bound;            /* Has been successfully bound */
  216.             SocketAddress    boundTo;        /* Has been successfully bound */
  217.  
  218.     class NetworkStack         *stack;            /* Underlying network interface */
  219.  
  220. private:
  221.  
  222.     static SocketResult     SpinStub        (class Socket *socket, 
  223.                                                 void *refcon);
  224.     /* Callbacks */
  225.             SocketSpinProc    spinProc;        /* Spin routine chain and data */
  226.             void *            spinRefcon;        /* Spin routine chain and data */
  227.  
  228. #ifdef _DEBUG
  229.     /* Packet logging functions */
  230.             Bool8            logEnabled;
  231.             Bool8            logInited;
  232.             Bool8            logReplace;
  233.             Char8            logName[ARBITRARY_BUFFER_SIZE];
  234.             Char8            *logBuffers[2];
  235.             Char8            *curLogBuffer;
  236.             SocketSpinProc    logSpinProc;
  237.             void *            logSpinRefcon;
  238.             DatagramReadProc logReadProc;
  239.             void *            logReadRefcon;
  240.             DatagramWriteProc logWriteProc;
  241.             void *            logWriteRefcon;
  242.  
  243.     static SocketResult     LoggingSpinStub    (class Socket *socket, 
  244.                                                 void *refcon);
  245.     static SocketResult     LoggingDatagramReadStub(class Socket *socket,
  246.                                                 void *refcon,
  247.                                                 Byte8 *dataPtr, UInt32 dataLen,
  248.                                                 SocketAddressPtr address);
  249.     static SocketResult     LoggingDatagramWriteStub(class Socket *socket,
  250.                                                 void *refcon,
  251.                                                 Byte8 *dataPtr, UInt32 dataLen,
  252.                                                 SocketAddressPtr address);
  253.             void            LoggingConvertBytes(Byte8 *dataPtr, UInt32 dataLen, 
  254.                                                     Char8 *addr, Char8 *hex,
  255.                                                     Char8 *ascii);
  256. #endif
  257.  
  258.     } Socket, *SocketRef;
  259.  
  260. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  261. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  262. /* Function Prototypes */
  263.  
  264.  
  265. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  266. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  267.  
  268. #endif /* __SOCKET_HEADER__ */
  269.  
  270.